/*****************************************************************************
 * hal_stm32f1_flash.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hal/source/hal_stm32f1/hal_stm32f1.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define FLASH_BACK_1_BASE 0x08000000

#define FLASH_BACK_2_BASE

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_flash_read(plum_u32 id, plum_u32 offset, plum_u8 *rData,
                             plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("the id is not a flash id");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (!rData || (size == 0)) {
            LOG_E("the paramter is err");
            rc = PLUM_ECODE_EPARAM;
            break;
        }

        plum_u32 loop;
        for (loop = 0; loop < size; loop++) {
            rData[loop] = *(plum_u8 *)(FLASH_BACK_1_BASE + offset);
        }

        rc = size;
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_write(plum_u32 id, plum_u32 offset, plum_u8 *wData,
                              plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("the id is not a flash id");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (!wData || (size == 0)) {
            LOG_E("the paramter is err");
            rc = PLUM_ECODE_EPARAM;
            break;
        }

        plum_u32 loop;
        plum_u16 data = 0;
        for (loop = 0; loop < size;) {
            data = *(plum_u16 *)wData;
            FLASH_Program_HalfWord(FLASH_BACK_1_BASE + offset, data);
            wData += 2;
        }

        rc = loop;
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_erase(plum_u32 id, plum_u32 offset, plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("the id is not a flash id");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        plum_u32 page_idx = offset / FLASH_PAGE_SIZE;
        plum_u32 page_cnt =
            size / FLASH_PAGE_SIZE + ((size % FLASH_PAGE_SIZE) ? 1 : 0);
        plum_u32 loop;
        for (loop = 0; loop < page_cnt; loop++) {
            Flash_PageErase(FLASH_BACK_1_BASE +
                            (page_idx + loop) * FLASH_PAGE_SIZE);
        }

    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
